$(document).ready(function() {
    queryGoodsDetail();
});

var keywords = getUrlParms("keywords");
console.log("keyword=="+keywords);
var sku;
var ipAddress="localhost";
var goodsName;
var queryGoodsDetail = function(){
    $("#category-name").html(keywords);
    $.ajax({
        url: "http://localhost:8002/goods/getSpu/"+keywords,
        type: "GET",
        async: true,
        data: null,
        dataType: "json",
        success: function (data) {
            if (data.code===201){
                console.log(data)
                var spuId = data.data.id;
                var spu = data.data;
                $.ajax({
                    url: "http://localhost:8002/goods/getSku/"+spuId,
                    type: "GET",
                    async: true,
                    data: null,
                    dataType: "json",
                    success: function (data) {
                        if (data.code===201) {
                            console.log(data)
                            var goods = data.data;
                                $("#detail").append(
                                   " <div class=\"left fl\"><img width='560px' src="+goods[0].picture+"></div>\n" +
                                    "    <div class=\"right fr\">\n" +
                                    "        <div class=\"h3 ml20 mt20\">"+keywords+"</div>\n" +
                                    "        <div class=\"jianjie mr40 ml20 mt10\">"+spu.spuIntro+"</div>\n" +
                                    "        <div class=\"jiage ml20 mt10\">"+spu.lowPrice+"元起</div>" +
                                    "        <div id='attr'></div>"+

                                    "        <div class=\"xqxq mt20 ml20\">\n" +
                                    "            <div class=\"top1 mt10\">\n" +
                                    "                <div class=\"left1 fl\" id='buyName'></div>\n" +
                                    "                <div class=\"right1 fr\" id='price'></div>\n" +
                                    "                <div class=\"clear\"></div>\n" +
                                    "            </div>\n" +
                                    "            <div class=\"bot mt20 ft20 ftbc\" id='totalPrice'></div>\n" +
                                    "        </div>\n" +

                                    "        <div class=\"xiadan ml20 mt20\">\n" +
                                    "            <input class=\"jrgwc\" type=\"button\" name=\"jrgwc\" value=\"收藏\" />\n" +
                                    "            <input class=\"jrgwc\" type=\"button\" name=\"jrgwc\" value=\"加入购物车\" onclick='addIntoCart(this)'/>\n" +

                                    "        </div>\n" +
                                    "    </div>\n" +
                                    "    <div class=\"clear\"></div>"
                                )

                            queryGoodsSpec(spuId);

                            $("input[name='jrgwc']").attr({
                                "class":"disable"
                            });


                        }else{
                            layer.msg("加载失败!");
                        }
                    },
                    error: function () {
                        layer.msg("加载失败!");
                    }
                })


            } else {
                layer.msg("加载失败!");
            }

        },
        error: function () {
            layer.msg("加载失败!");
        }
    })
};



var queryGoodsSpec = function (spuId) {

    $.ajax({
        url: "http://localhost:8002/goods/goodsDetail/"+spuId,
        type: "GET",
        async: true,
        data: null,
        dataType: "json",
        success: function (data) {
            if (data.code===201) {
                console.log("da:"+data.data);
                for(var key in data.data){  //通过定义一个局部变量k遍历获取到了map中所有的key值
                    var value=data.data[key]; //获取到了key所对应的value的值！
                    console.log("docList"+value)
                    console.log("docList"+key)
                    $("#attr").append(
                    "        <div class=\"ft20 ml20 mt20\" >选择"+key+"</div>\n" +
                    "        <div class=\"xzbb ml20 mt10\" >\n" +
                    "        <div id="+key+"></div>"+
                    "        <div class=\"clear\"></div>\n" +
                    "        </div>\n"
                    )
                    if (key==="内存"){
                        for (var i=0;i<value.length;i++){
                            $("#"+key).append(
                                "            <div class=\"banben fl\" onclick='choose(this)' id="+key+i+">\n" +
                                "                <a >\n" +
                                "                    <span class='version'>"+value[i]+"</span>\n" +
                                "                </a>\n" +
                                "            </div>"
                            )
                        }
                    } else{
                        for (var i=0;i<value.length;i++){

                            $("#"+key).append(
                                "            <div class=\"banben fl\" onclick='choose(this)' id="+key+i+">\n" +
                                "                <a>\n" +
                                "                    <span class="+value[i]+"></span>"+
                                "                    <span>"+value[i]+"</span>\n" +
                                "                </a>\n" +
                                "            </div>"
                            )
                        }
                    }
                }

            }else{
                layer.msg("加载失败!");
            }
        },
        error: function () {
            layer.msg("加载失败!");
        }
    })
};
var color = "";
var neicun = "";


var choose = function (obj) {
    var id = obj.id;
    var version =  $(obj).children("a").children("span").text();
    $("#buyName").html("")
    $("#totalPrice").html("")
    if (version.indexOf("色")>0){
        color =  version;
        console.log("se==="+color)
    }else if (version.indexOf("G")>0) {
        neicun =  version;
        console.log("neicun==="+neicun)
    }
    goodsName = keywords+" " + neicun+" "+color;
    console.log("goodsName="+goodsName);
    $("#buyName").html("您已选择 "+"<span style='color: #ff6709'>"+goodsName+"</span>")
    if (goodsName.indexOf("色")>0&&goodsName.indexOf("G")>0) {
        $.ajax({
            url: "http://localhost:8002/goods/getSkuByName/" + goodsName,
            type: "GET",
            async: true,
            data: null,
            dataType: "json",
            success: function (data) {
                if (data.code === 201) {
                    //var sku = data.data;
                     sku = data.data;
                    console.log("stock==="+sku.stock)
                    if (sku.stock<=0||sku.stock==="暂未开售"){
                        $("input[name='jrgwc']").attr({
                            "class":"disable"
                        });
                        $("#totalPrice").html("暂时无货");

                    }else {
                        $("input[name='jrgwc']").attr({
                            "class":"jrgwc",
                            "disable":"true"
                        });
                        $("#totalPrice").html("共计 "+"<span style='color: #ff6709'>"+sku.price+"元</span>")
                    }
                }
            }, error: function () {
                layer.msg("网络错误")
            }
        })
    }

    if ($(obj).attr("border-color")!="#ff6700"){
        $($(obj).children("a").children()).css({
            "color":"#ff6700"
        });
        $("#"+id).css({
            "border-color":"#ff6700"
        });
        $(obj).siblings().each(function (index,value) {
           $(value).css({
               "border-color":""
           })
            $($(value).children("a").children()).css({
                "color":""
            });
        });

    }


}


//验证用户是否登录
var validateUser=function(){
    $.ajax({
        //url: "http://" + ipAddress + ":8080/user/get_user_info",
        url: "http://"+ipAddress+":8004/orderInfo/checkAccount",
        type: "GET",
        async: true,
        data: null,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            console.log(" data====  " + data);
            if (data.message =="success") {
                userName=data.data.nikename;
                getMyOrderInfo(9);
            }else{
                layer.msg("请先登录！");
                setTimeout(function(){//1秒后跳转
                    window.location.href = "./login.html"
                },1000);
            }
        },
        error: function () {
            layer.msg("后台服务器开小差了...")
        }
    })
};






//加入购物车
var addIntoCart = function (obj) {
    if($("#buyName").html()==""||$("#buyName").html()==null){
        layer.msg("请先选择商品规格!");
    }else if($("#totalPrice").html()=="暂时无货"){
        layer.msg("")
    }else {
        var cart={
            accountId:"1",
            skuId:sku.id,
            spuId:sku.spuId,
            goodsName:goodsName,
            price:sku.price,
            number:1
        }
        console.log("cart=="+cart.accountId);
        console.log("cart=="+cart.skuId);
        console.log("cart=="+cart.spuId);
        console.log("cart=="+cart.goodsName);
        console.log("cart=="+cart.price);

        $.ajax({
            url: "http://"+ipAddress+":8003/cart/cart/",
            type: "POST",
            async: true,
            data: JSON.stringify(cart),
            contentType:"application/json",
            dataType: "json",
            success:function (data) {

                console.log(data.code);
                if(data.message=="请先登录！"){
                    layer.msg("请先登录！");
                    setTimeout(function(){//两秒后跳转
                        window.location.href = "./login.html";
                    },2000);
                }else{
                    if (data.code===false) {
                        layer.msg("服务器开小差了!"+data.message)
                    }else{
                        if(data.message=="添加失败!该宝贝限购哦！"){
                            layer.msg("您购物车中该宝贝已到达限购额！");
                        }else{
                            layer.msg("已成功加入购物车！两秒后跳转");
                            setTimeout(function(){//两秒后跳转
                                window.location.href = "./cart.html";
                            },2000);
                        }
                    }
                }
            },
            error:function () {
                layer.message("添加购物车失败!"+data.data.message);
            }
        });
    }
}




